/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.testile.dataset.sqlscript;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import javax.script.ScriptEngine;
import javax.script.ScriptEngineFactory;

/**
 *
 * @author hudimar
 */
public class SqlScriptEngineFactory implements ScriptEngineFactory {

	private static List<String> names;
	private static List<String> extensions;
	private static List<String> mimeTypes;

	static {
		names = new ArrayList<String>(1);
		names.add("sql");
		names = Collections.unmodifiableList(names);
		extensions = names;
		mimeTypes = new ArrayList<String>(0);
		mimeTypes = Collections.unmodifiableList(mimeTypes);
	}

	public String getEngineName() {
		return "sql";
	}

	public String getEngineVersion() {
		return "0.1";
	}

	public List<String> getExtensions() {
		return extensions;
	}

	public String getLanguageName() {
		return "sql";
	}

	public String getLanguageVersion() {
		return "sql 92?";
	}

	public String getMethodCallSyntax(String obj, String m, String... args) {
		StringBuffer buf = new StringBuffer();
		buf.append("call " + m + " ");
		buf.append(" on " + obj + " with ");
		for (int i = 0; i < args.length; i++) {
			buf.append(args[i] + ", ");
		}
		buf.append(";");
		return buf.toString();
	}

	public List<String> getMimeTypes() {
		return mimeTypes;
	}

	public List<String> getNames() {
		return names;
	}

	public String getOutputStatement(String toDisplay) {
		return "output: " + toDisplay;
	}

	public Object getParameter(String key) {
		// map
		if (key.equals(ScriptEngine.ENGINE)) {
			return getEngineName();
		} else if (key.equals(ScriptEngine.ENGINE_VERSION)) {
			return getEngineVersion();
		} else if (key.equals(ScriptEngine.NAME)) {
			return getEngineName();
		} else if (key.equals(ScriptEngine.LANGUAGE)) {
			return getLanguageName();
		} else if (key.equals(ScriptEngine.LANGUAGE_VERSION)) {
			return getLanguageVersion();
		} else {
			return null;
		}
	}

	public String getProgram(String... statements) {
		StringBuffer buf = new StringBuffer();
		for (int i = 0; i < statements.length; i++) {
			buf.append(statements[i]);
		}
		return buf.toString();
	}

	public ScriptEngine getScriptEngine() {
		return new SqlScriptEngine();
	}
}
